<template>
  <div>
    <el-dialog
      title="提示"
      :visible.sync="dialogVisible"
      width="50%"
      @close="handleClose"
    >
      <span>
        <el-form
          ref="ruleForms"
          :model="ruleFormz"
          :rules="rules"
          label-width="100px"
          class="demo-ruleForm"
        >
          <el-form-item label="角色名称" prop="name">
            <el-input v-model="ruleFormz.name" class="froms" />
          </el-form-item>
          <el-form-item label="角色描述" prop="description">
            <el-input v-model="ruleFormz.description" class="froms" />
          </el-form-item>
        </el-form>
      </span>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="confirm">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { add, updateRole } from '@/api/serring'
export default {
  name: 'VueAdminTemplateBounced',
  props: {
    state: {
      type: Boolean,
      default: false
    },

    isAll: {
      type: Boolean,
      default: null
    },
    row: {
      type: Object,
      default: () => {}
    }
  },
  data() {
    return {
      ruleFormz: {
        name: '',
        description: '',
        id: ''
      },

      rules: {
        name: [{ required: true, message: '请输入角色名称', trigger: 'blur' }],
        description: [
          { required: true, message: '请输入角色描述', trigger: 'blur' }
        ]
      },
      dialogVisible: false
    }
  },
  watch: {
    state: {
      handler(val) {
        console.log('变化')
        this.dialogVisible = val
      },
      immediate: true
    },

    isAll: {
      handler(val) {
        if (val) {
          console.log(val)
        } else {
          console.log('周开心')
        }
      },
      immediate: true
    },
    row: {
      handler(vl) {
        console.log('binahua')
        this.ruleFormz = vl
        console.log(this.ruleFormz)
      },
      immediate: true
    }
  },
  mounted() {},

  methods: {
    // 确认
    confirm() {
      this.$refs.ruleForms.validate(async(valid) => {
        if (valid) {
          if (this.isAll) {
            console.log(this.ruleFormz)
            const res = await updateRole(this.ruleFormz)
            console.log(res)
          } else {
            const res = await add(this.ruleFormz)
            console.log(res)
          }
          this.$emit('gx')
          this.dialogVisible = false
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    // 关闭
    handleClose() {
      this.dialogVisible = false
      this.$emit('update:state', this.dialogVisible)
      this.$emit('update:isAll', false)
      this.$refs.ruleForms.resetFields()
    }
  }
}
</script>

<style lang="scss" scoped>
</style>
